/*
 * @Author: 胡俊/treence
 * @Date: 2022-05-27 15:48:51
 * @LastEditors: 黄宇/Hyuishine
 * @LastEditTime: 2024-08-22 14:59:53
 * @Description:
 * @Email: treence@gmail.com
 * @Company: 3xData
 * @youWant: add you want
 */

import {
  unitList,
  unitForm,
  checkFace,
  staffForm,
  staffUpdate,
  delTask,
  addTask,
  updateTask,
  getObtainLongitude,
  uploadObtainLongitude,
  getUserInfo,
  judgmentLevelList,
  confirmTask,
  taskExcuteRecord,
  faceTest,
  getUnitListApi,
  getUnConfirmApi,
  getDoneTaskApi,
  getExpireTaskApi,
} from "@/api/unitTask.js";

const unitTask = {
  state: {
    taskId: "", // 任务id
    staffId: "", // 执业人员id
    projectId: "", // 项目id
    lonLat: "", // 项目经纬度
    fileUrls: null, // 图片路径
    imageFile: "", // 现场照片
    type: [], // 树结构
    wbName: "", // 执业内容
    projectHead: "", // staffId
    projectLeader: null, // 项目负责人识别人脸后缓存下来的字段
    portraitCollection: null, // 添加人员时的人脸识别后缓存下来的字段

    executeCoordinate: null, // 当执业时，后端未返回执业地址，需要将所有数据缓存后，等执业人员将执业地址存储起来传给后端

    form: {
      projectLeader: null, // 项目负责人
      fileUrls: null, // 执业人员
      examineLeader: null, // 审核人脸照片
    },
  },
  mutations: {
    save_area(state, data) {
      state.area = data;
    },
    save_url(state, data) {
      state.projectLeader = data;
    },
    save_taskId(state, data) {
      state.taskId = data;
    },
    save_staffId(state, data) {
      state.projectHead = data;
    },
    save_examine(state, data) {
      state.form[data.key] = data.value;
    },
  },
  actions: {
    unitList({ commit }, obj) {
      return new Promise((resolve, reject) => {
        unitList(obj, function (data) {
          resolve(data);
        });
      });
    },
    getUnitList({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getUnitListApi(obj, function (data) {
          resolve(data);
        });
      });
    },
    getUnConfirm({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getUnConfirmApi(obj, function (data) {
          resolve(data);
        });
      });
    },
    getDoneTask({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getDoneTaskApi(obj, function (data) {
          resolve(data);
        });
      });
    },
    getExpireTask({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getExpireTaskApi(obj, function (data) {
          resolve(data);
        });
      });
    },

    unitForm({ commit }, obj) {
      return new Promise((resolve, reject) => {
        unitForm(obj, function (data) {
          resolve(data);
        });
      });
    },
    // 获取经纬度和项目负责人id
    ObtainLongitudeAndLatitude({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getObtainLongitude(obj, function (data) {
          resolve(data);
        });
      });
    },

    // 判断当前用户的级别是否为中级及中级以上
    judgmentLevel({ commit }, obj) {
      return new Promise((resolve, reject) => {
        judgmentLevelList(obj, function (data) {
          resolve(data);
        });
      });
    },

    // 上传当前位置的经纬度，返回至为true或者fasle
    uploadObtain({ commit }, obj) {
      return new Promise((resolve, reject) => {
        try {
          let { origins, destination, placeRange } = obj;

          // 因为腾讯地图经纬度坐标是反过来的，[纬度，经度] 实际应该是[纬度，经度]
          origins = origins
            .split(",")
            .map((item) => Number(item))
            .reverse();
          destination = destination
            .split(",")
            .map((item) => Number(item))
            .reverse();

          // 摘抄自 openlayers ol/sphere.js => getDistance
          const radius = 6371008.8;

          function toRadians(angleInDegrees) {
            return (angleInDegrees * Math.PI) / 180;
          }

          const lat1 = toRadians(origins[1]);
          const lat2 = toRadians(destination[1]);
          const deltaLatBy2 = (lat2 - lat1) / 2;
          const deltaLonBy2 = toRadians(destination[0] - origins[0]) / 2;
          const a =
            Math.sin(deltaLatBy2) * Math.sin(deltaLatBy2) +
            Math.sin(deltaLonBy2) *
              Math.sin(deltaLonBy2) *
              Math.cos(lat1) *
              Math.cos(lat2);

          const distance =
            2 * radius * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

          resolve({
            code: 200,
            data: {
              result: distance < placeRange,
              distance,
            },
            msg: "操作成功",
          });
        } catch (error) {
          reject({
            code: 200,
            data: {
              result: false,
              distance,
              error,
            },
            msg: "计算失败",
          });
        }
        // uploadObtainLongitude(obj, function (data) {
        //     resolve(data)
        // })
      });
    },

    getUserInfoInterface({ commit }, obj) {
      return new Promise((resolve, reject) => {
        getUserInfo(obj, function (data) {
          resolve(data);
        });
      });
    },

    checkFace({ commit }, obj) {
      return new Promise((resolve, reject) => {
        checkFace(obj, function (data) {
          resolve(data);
        });
      });
    },
    staffForm({ commit }, obj) {
      return new Promise((resolve, reject) => {
        staffForm(obj, function (data) {
          resolve(data);
        });
      });
    },
    staffUpdate({ commit }, obj) {
      return new Promise((resolve, reject) => {
        staffUpdate(obj, function (data) {
          resolve(data);
        });
      });
    },
    delTask({ commit }, obj) {
      return new Promise((resolve, reject) => {
        delTask(obj, function (data) {
          resolve(data);
        });
      });
    },
    addTask({ commit }, obj) {
      return new Promise((resolve, reject) => {
        addTask(obj, function (data) {
          resolve(data);
        });
      });
    },
    updateTask({ commit }, obj) {
      return new Promise((resolve, reject) => {
        updateTask(obj, function (data) {
          resolve(data);
        });
      });
    },

    confirmTaskList({ commit }, obj) {
      return new Promise((resolve, reject) => {
        confirmTask(obj, function (data) {
          resolve(data);
        });
      });
    },
    taskExcuteRecordList({ commit }, obj) {
      return new Promise((resolve, reject) => {
        taskExcuteRecord(obj, function (data) {
          resolve(data);
        });
      });
    },
    setFaceTest({ commit }, obj) {
      return new Promise((resolve, reject) => {
        faceTest(obj, function (data) {
          resolve(data);
        });
      });
    },
  },
};

export default unitTask;
